﻿#include "EagleDeviceMgr.h"
#include "ConfigFile.h"
#include "EagleDefine.h"
#include "EagleError_ControlApi_CErrorCode.h"
#include "EagleMainFrame.h"

CEagleDeviceMgr::CEagleDeviceMgr() : refresh_status_thread(&CEagleDeviceMgr::fetch_thread, this)
, thread_destroyed(false)
, fetch_thread_paused(false)
{

}


CEagleDeviceMgr::~CEagleDeviceMgr()
{
}

void CEagleDeviceMgr::ClearDevices()
{
	device_lock.lock();
	device_list.clear();
	device_check_list.clear();
	diskThreshold.clear();
	SDI_Camera_mode_map.clear();
	device_lock.unlock();
	if (callback)
		callback();

	device_status_lock.lock();
	device_system_status.clear();
	device_capture_status.clear();
	device_disk_config_status.clear();
	device_connection_status.clear();
	device_status_lock.unlock();
}

eagle_system_status CEagleDeviceMgr::GetDeviceStatus(int device_id) 
{

	try{
		return device_system_status.at(device_id);
	}
	catch (std::out_of_range&)
	{
		eagle_system_status status;
		memset(&status, 0x00, sizeof(eagle_system_status));
		return status;
	}

}

eagle_camera_disk_config CEagleDeviceMgr::GetDeviceDiskConfig(int device_id)
{ 
	try{
		return device_disk_config_status.at(device_id); 
	}
	catch (std::out_of_range&)
	{
		eagle_camera_disk_config disk_config;
		memset(&disk_config, 0x00, sizeof(eagle_camera_disk_config));
		return disk_config;
	}
}

eagle_capture_config CEagleDeviceMgr::GetDeviceCcdCaptureStatus(int device_id, int ccd_no)
{ 
	
	try{
		return device_capture_status.at(device_id).at(ccd_no);
	}
	catch (std::out_of_range&)
	{
		eagle_capture_config ccd_config;
		memset(&ccd_config, 0x00, sizeof(eagle_camera_disk_config));
		ccd_config.camera_no = ccd_no;
		return ccd_config;
	}
}


bool CEagleDeviceMgr::GetOneDeviceConnectionStatus(int device_id)
{
	try{
		return std::get<0>(device_connection_status.at(device_id));
	}
	catch (std::out_of_range&)
	{
		return false;
	}
}

bool CEagleDeviceMgr::GetConnectionStatus()
{
	bool online = false;
	for (auto it = device_connection_status.begin(); it != device_connection_status.end(); it++)
	{
		if (std::get<0>(it->second) == true)
		{
			online = true;
			break;
		}
	}
	return online;
}

void CEagleDeviceMgr::fetch_thread()
{
//	int fails = 0;
	std::this_thread::sleep_for(std::chrono::milliseconds(3000));
	bool dev_inline = true;

	while (1)
	{
		for (int i = 0; i < 20; i++)
		{
			if (thread_destroyed)
				break;
			std::this_thread::sleep_for(std::chrono::milliseconds(100));
		}
		if (thread_destroyed)
			break;
		if (fetch_thread_paused)
		{
			std::this_thread::sleep_for(std::chrono::milliseconds(2000));
			continue;
		}
		// 所有设备都断开[11/22/2016 ejitec]
		if (GetConnectionStatus() == false)
		{
			std::this_thread::sleep_for(std::chrono::milliseconds(2000));
		}
		std::vector<int> device_list;
		GetAllDevice(device_list);
		eagle_system_status system_status;
		eagle_camera_disk_config disk_mapping;
		device_status_lock.lock();
		for (auto it = device_list.begin(); it != device_list.end(); it++)
		{
			dev_inline = true;
			std::tuple<bool, int>& tu = device_connection_status.at(*it);
			int ret0 = EagleControl_GetSystemStatus(*it, &system_status);
			if (ret0 == EAGLE_SDK_STATUS_SUCCESS)
			{
				device_system_status[*it] = system_status;
				int camera_num = 0;
				int ret = EagleControl_GetMaxCameraNumber(*it, &camera_num);
				std::get<1>(tu) = 0;
				std::get<0>(tu) = true;
			}
			else if (ret0 == Receive_Data_Timeout)
			{
				std::get<1>(tu) += 1;
				if (std::get<1>(tu) >= 2)
				{
					std::get<0>(tu) = false;
				}
				
				continue;
			}
			ret0 = EagleControl_GetCameraDiskMapping(*it, &disk_mapping);
			if (ret0 == EAGLE_SDK_STATUS_SUCCESS)
			{				
				device_disk_config_status[*it] = disk_mapping;
				std::get<0>(tu) = true;
				std::get<1>(tu) = 0;
			}
			else if (ret0 == Receive_Data_Timeout)
			{
				std::get<1>(tu) += 1;
				if (std::get<1>(tu) >= 2)
				{
					std::get<0>(tu) = false;
				}

				continue;
			}

			line_status_lock.lock();
			// new Add [10/26/2016 ejitec]
			TLineStatus tfrebStatus = { 0 };
			ret0 = EagleControl_GetDeviceCameraStatus(*it, &(tfrebStatus.m_LineStatus));
			tfrebStatus.m_bOPRet = (EAGLE_SDK_STATUS_SUCCESS == ret0);
			device_line_staus[*it] = tfrebStatus;
			line_status_lock.unlock();
				
			int camera_num = 0;
			EagleControl_GetMaxCameraNumber(*it, &camera_num);

			bool device_time_out = false;

			for (int i = 1; i <= camera_num; i++)
			{
				if (EagleControl_IsCameraValid(*it, i)) {
					eagle_capture_config& cap_config = device_capture_status[*it][i];
					cap_config.camera_no = i;
					ret0 = EagleControl_GetCameraCaptureParam(*it, &cap_config);
					if (ret0 == Receive_Data_Timeout)
					{
						device_time_out = true;
						break;
					}
					eagle_capture_config detect_param;
					detect_param.camera_no = i;
					ret0 = EagleControl_GetCameraDetectedParam(*it, &detect_param);
					if (ret0 == Receive_Data_Timeout)
					{
						device_time_out = true;
						break;
					}

					MessageCmd cmd;
					try {
						MessageCmd* ptcmd = new MessageCmd(GT_CMD_SEND_CCD_DETECT_INFO);
						MessageCmd& cmd = *ptcmd;
						cmd.cmd = GT_CMD_SEND_CCD_DETECT_INFO;
						cmd.lose_frame = 0;
						if (EagleConfigFile::getInstance()->enable_display_framelost_signal == "1") {
							if (((system_status.frame_is_successional >> (i - 1)) & 0x01) == 0x01)
							{
								cmd.lose_frame = 1;
							}
						}
						cmd.column = detect_param.column;
						cmd.line = detect_param.line;
						cmd.frame_rate = detect_param.frame_rate;
						g_theApp->postMessageBy(*it, i, ptcmd);
					}
					catch (std::exception)
					{
						continue;
					}
				}
			}
			// 连续2次 将置位
			if (device_time_out)
			{
				std::get<1>(tu) += 1;
				if (std::get<1>(tu) >= 2)
				{
					std::get<0>(tu) = false;
					std::get<1>(tu) = 0;
				}
				dev_inline = false;
			}
			else
			{
				if (std::get<0>(tu) == false)  // 上次是断开的 则重新扫描记录列表
				{
					EagleData_RefetchRecList();
				}

				std::get<1>(tu) = 0;
				std::get<0>(tu) = true;
			}
			//std::get<0>(tu) = dev_inline;
// 			if (device_time_out)
// 				dev_inline = false;
// 			if (std::get<0>(tu) == false && dev_inline == true)
// 			{
// 				EagleData_RefetchRecList();
// 			}
// 			std::get<0>(tu) = dev_inline;
		} // dev_it
		for (auto timeout_it = device_connection_status.begin(); timeout_it != device_connection_status.end(); timeout_it++)
		{
			if (std::get<0>(timeout_it->second) == false) {
				try{
					// dev fpga信息
					memset(&device_system_status.at(timeout_it->first), 0x00, sizeof(eagle_system_status));
					// 磁盘映射
					memset(&device_disk_config_status.at(timeout_it->first), 0x00, sizeof(eagle_camera_disk_config));
					for (auto ccd_it = device_capture_status.at(timeout_it->first).begin(); ccd_it != device_capture_status.at(timeout_it->first).end(); ccd_it++)
					{
						// ccd 采集参数 状态
						memset(&ccd_it->second, 0x00, sizeof(eagle_capture_config));
					}
				}
				catch (std::out_of_range&)
				{
					continue;
				}
			}

		}
		device_status_lock.unlock();		
	}

	qDebug() << "CEagleDeviceMgr fetch_thread quitted ...";
}

bool CEagleDeviceMgr::GetAllDevice(std::vector<int> &device)
{
	device_lock.lock();
	if (device_list.empty())
	{
		device_lock.unlock();
		return false;
	}
	device.clear();
	for (auto it = device_list.begin(); it != device_list.end(); it++)
	{
		device.push_back(*it);
	}
	device_lock.unlock();
	return true;
}

bool CEagleDeviceMgr::GetCheckedDeviceList(std::vector<int> &device)
{
	device_lock.lock();
	device.clear();
	for (auto it = device_list.begin(); it != device_list.end(); it++)
	{
		if (device_check_list[*it] == true)
			device.push_back(*it);
	}
	device_lock.unlock();
	return true;
}

bool CEagleDeviceMgr::RescanDevices()
{
	ClearDevices();
	EagleDataMgr::GetInstance()->ClearRecordList();

	int nDeviceCount = 0;
	//
	device_lock.lock();
	int ret = EagleControl_ScanAndGetDeviceNum(&nDeviceCount);
	if (ret != 0 || nDeviceCount == 0)
	{
		device_lock.unlock();
		return false;
	}

	std::vector<int> vDevs(nDeviceCount);
	int acutal_ids = 0;
	ret = EagleControl_GetDeviceIds(vDevs.data(), nDeviceCount, &acutal_ids);
	vDevs.erase(vDevs.begin() + acutal_ids, vDevs.end());


	char seriala[32] = { 0 };
	char serialb[32] = { 0 };

	QStringList sort_name = EagleConfigFile::getInstance()->device_sort_order;

	// 根据配置文件指定的设备顺序 进行设备排序 [11/18/2016 ejitec]
	if (sort_name.size() != 0 && vDevs.size() > 1)
	{
		std::stable_sort(vDevs.begin(), vDevs.end(), [&](int a, int b)->bool
		{
			memset(seriala, 0, sizeof(seriala));
			memset(serialb, 0, sizeof(serialb));
			EagleControl_GetDeviceSerial(a, seriala);
			EagleControl_GetDeviceSerial(b, serialb);
			// true 的 放前面
			return (quint32)(sort_name.indexOf(seriala)) < (quint32)(sort_name.indexOf(serialb));
		});
	}

	int device_id = -1;
	for (int i = 0; i < acutal_ids; i++)
	{
		TDevWithCCDInfo* ptDevInfo = new TDevWithCCDInfo();
		wchar_t serial[32] = { '\0' };

		device_id = vDevs[i];
		device_list.push_back(device_id);  // 按序保存设备队列 
		device_connection_status[device_id] = std::make_tuple(true, 0);
		bool device_is_checked = false;

		diskThreshold[device_id] = EagleConfigFile::getInstance()->disk_threshold;
		PIXEL_SAMPLING_TYPE type = PixelSamplingGrey;
		EDeviceType device_type =  EagleControl_GetDeviceType(device_id);
		if (device_type == EDeviceTypeSDI)
		{
			type = PixelSamplingYCbCr422i;
		}
		EagleControl_GetDeviceName(device_id, serial);
		ptDevInfo->m_nDevID = device_id;
		ptDevInfo->m_strDevNam = QString::fromWCharArray(serial);
		ptDevInfo->m_device_type = device_type;
		SDI_Camera_mode_map[device_id] = type;

		ret = EagleControl_GetSystemStatus(device_id, &ptDevInfo->m_tStatus);
		ret = EagleControl_GetCameraDiskMapping(device_id, &ptDevInfo->m_diskMap);
		int camera_num = 0;
		EagleControl_GetMaxCameraNumber(device_id, &camera_num);
		for (int j = 0; j < camera_num; j++)
		{
			TCCDInfo tCCDInfo;
			wchar_t ccd_name[32] = { '\0' };

			tCCDInfo.m_tInfo.camera_no = j + 1;
			bool camera_checked = EagleControl_IsCameraChecked(device_id, j + 1);
			bool camera_valid = EagleControl_IsCameraValid(device_id, j + 1);
			// 防止出现相机无效但是勾线选中的状态
			if (!camera_valid && camera_checked)
			{
				camera_checked = false;
				EagleControl_CheckCamera(device_id, j + 1, camera_checked);
			}
			if (camera_checked)
				device_is_checked = true;

			if (!EagleControl_IsCameraValid(device_id, j + 1))
			{
				tCCDInfo.m_bValid = false;
			}

			EagleControl_GetCameraName(device_id, j + 1, ccd_name);
			tCCDInfo.m_strCCDNam = QString::fromWCharArray(ccd_name);
			EagleControl_GetCameraCaptureParam(device_id, &tCCDInfo.m_tInfo);
			ptDevInfo->m_vCCDInfo.push_back(tCCDInfo);
		}

		device_check_list[device_id] = device_is_checked;

		qDebug() << "Deviec" << i << "setting ok ...";
		// 判断网卡MTU是否为巨型帧，如果不是，设置设备实时显示网络包大小
		int nic_mtu = 0;
		if (EagleControl_GetNICMtu(device_id, &nic_mtu) == EAGLE_SDK_STATUS_SUCCESS)
		{
			qInfo() << "EagleControl_GetNICMtu:" << nic_mtu;
			if (nic_mtu < 8000)
			{
				EagleControl_SetRealtimePackageSize(device_id, nic_mtu / 1024);
			}
		}

#ifdef CHENGGS_VERSION

		long column, line, color_depth, taps;
		eagle_capture_config cap_config;
		memset(&cap_config, 0x00, sizeof(cap_config));
		for (int i = 1; i <= 4; i++)  // 成光的设备对应的相机 参数单独设置
		{
			if (i == 2 && CHENGGS_VERSION == 2)  // 2期
			{
				EagleControl_SetDeviceCameraValid(device_id, i, false);
				continue;
			}

			if (i == 4 && CHENGGS_VERSION == 3)  // 3 期
			{
				EagleControl_SetDeviceCameraValid(device_id, i, false);
				continue;
			}

			CcdConfig ccd_config = EagleConfigFile::getInstance()->ccd_config.at(QString::number(i));
			column = ccd_config.pix_col.toLong();
			line = ccd_config.pix_line.toLong();
			color_depth = ccd_config.color_depth.toLong();
			taps = ccd_config.taps.toLong();
			cap_config.camera_no = i;
			cap_config.column = column;
			cap_config.line = line;
			cap_config.color_depth = color_depth;
			cap_config.taps = taps;

			if (i == 1 && CHENGGS_VERSION == 3)
			{
				continue;// MTV
			}

			EagleControl_SetCameraCaptureParam(device_id, &cap_config);
		}
		// 针对每台设备
		long size = EagleConfigFile::getInstance()->network_packet_size.toLong();
		EagleControl_SetRealtimePackageSize(device_id, size);
		wchar_t camera_name[32] = { '\0' };
		QString("MTV").toWCharArray(camera_name);
		EagleControl_SetCameraName(device_id, 1, camera_name);
		if (CHENGGS_VERSION == 2)
		{
			QString("CTV").toWCharArray(camera_name);
			EagleControl_SetCameraName(device_id, 3, camera_name);
			QString("MIR").toWCharArray(camera_name);
			EagleControl_SetCameraName(device_id, 4, camera_name);
		}
		else if (CHENGGS_VERSION == 3)
		{
			QString("CTV").toWCharArray(camera_name);
			EagleControl_SetCameraName(device_id, 2, camera_name);
			QString("MIR").toWCharArray(camera_name);
			EagleControl_SetCameraName(device_id, 3, camera_name);
		}
#endif

		g_theApp->m_vDevList.push_back(ptDevInfo);
	}
	device_lock.unlock();
	if (callback)
		callback();

	return true;
}

bool CEagleDeviceMgr::RescanDevices(eagle_all_netcards *nics)
{
	if (EagleControl_SetControlNICs(nics) != 0)
	{
		return false;
	}

	return RescanDevices();
}

bool CEagleDeviceMgr::IsDeviceChecked(int device_id)
{
	bool valid = false;
	try{
		valid = device_check_list.at(device_id);
	}
	catch (std::out_of_range& )
	{
	}
	return valid;
}

void CEagleDeviceMgr::ForceRefreshCallback()
{
	if (callback)
		callback();
}

bool CEagleDeviceMgr::SetDiskThreshold(int device_id, QString & newThreshold)
{
	diskThreshold[device_id] = newThreshold;
	return true;
}

ECaptureStatusType CEagleDeviceMgr::GetCurrentDeviceCaptureStatus(int device_id)
{
	int current_status = 0;

	std::vector<eagle_capture_config> ccd_list = this->GetCcdList(device_id);

	for (auto it = ccd_list.begin(); it != ccd_list.end(); it++)
	{
		// ECaptureStatusType
		if (it->capture_status == ECaptureRecording)
			return ECaptureRecording;
		current_status = it->capture_status > current_status ? it->capture_status : current_status;
	}
	
	return (ECaptureStatusType)(current_status >= ECaptureMax ? ECaptureReady : current_status);
}

ECaptureStatusType CEagleDeviceMgr::GetCurrentSystemCaptureStatus()
{
	int current_status = 0;

	for (auto dev_it = device_capture_status.begin(); dev_it != device_capture_status.end(); dev_it++)
	{
		for (auto it = dev_it->second.begin(); it != dev_it->second.end(); it++)
		{
			if (it->second.capture_status == ECaptureRecording)
				return ECaptureRecording;
			current_status = it->second.capture_status > current_status ? it->second.capture_status : current_status;
		}
	}
	return (ECaptureStatusType)(current_status >= ECaptureMax ? ECaptureReady : current_status);
}

void CEagleDeviceMgr::SetCheckedDeviceList(std::vector<int> &devices)
{
	bool check;
	for (auto dev_it = device_check_list.begin(); dev_it != device_check_list.end(); dev_it++)
	{
		check = false;
		for (auto chk_it = devices.begin(); chk_it != devices.end(); chk_it++)
		{
			if (*chk_it == dev_it->first)
			{
				check = true;
				break;
			}
		}
		dev_it->second = check;
	}
}

bool CEagleDeviceMgr::SetDeviceChecked(int device_id, bool bcheck)
{
	bool bres = false;
	for (auto dev_it = device_check_list.begin(); dev_it != device_check_list.end(); dev_it++)
	{
		if (device_id == dev_it->first)
		{
			dev_it->second = bcheck;
			bres = true;
			break;
		}
		
	}
	return bres;
}

std::vector<eagle_capture_config> CEagleDeviceMgr::GetCheckedCcdList(int device_id)
{
	eagle_capture_config ccd_config;
	std::vector<eagle_capture_config> res;
	int camera_num = 0;
//		device_lock.lock();
		EagleControl_GetMaxCameraNumber(device_id, &camera_num);
		for (int i = 1; i <= camera_num; i++)
		{
			memset(&ccd_config, 0, sizeof(ccd_config));
			if (EagleControl_IsCameraChecked(device_id, i) && EagleControl_IsCameraValid(device_id, i))
			{
				ccd_config.camera_no = i;
				if (EagleControl_GetCameraCaptureParam(device_id, &ccd_config) == 0)
					res.push_back(ccd_config);
			}
		}
//		device_lock.unlock();
	return res;
}


std::vector<eagle_capture_config> CEagleDeviceMgr::GetCcdList(int device_id)
{

	eagle_capture_config ccd_config;
	std::vector<eagle_capture_config> res;
	res.clear();
	int camera_num = 0;

//		device_lock.lock();
		EagleControl_GetMaxCameraNumber(device_id, &camera_num);
		for (int i = 1; camera_num > 0 && i <= camera_num; i++)
		{
			if (EagleControl_IsCameraValid(device_id, i) == false)
				continue;
			memset(&ccd_config, 0, sizeof(ccd_config));
			ccd_config.camera_no = i;
			if (EagleControl_GetCameraCaptureParam(device_id, &ccd_config) == 0)
				res.push_back(ccd_config);
		}
//		device_lock.unlock();
	
	return res;
}

bool CEagleDeviceMgr::GetAndSetPeerSerialCtrl(unsigned short& nValue, bool bToSet)
{
#ifdef _WIN32
	vector<int> devList;
	GetAllDevice(devList);

	if (devList.size() == 0)
	{
		return false;
	}

	bool bRet = (EagleControl_IsDeviceSerialControlEnable(*devList.begin(), &nValue) == 0);
	if (!bRet)
	{
		return false;
	}

	if (!bToSet)
	{
		return bRet;
	}
	// 设置相反的状态
	return EagleControl_EnableDeviceSerialControl(*devList.begin(),(nValue + 1)%2) == 0;
#else
	return true;
	
#endif
}

bool CEagleDeviceMgr::IsFrebOnLine(int devID, int ccdID,int& nret)
{
	std::unique_lock<mutex> lock(line_status_lock);
	
	try
	{
		auto it = device_line_staus.find(devID);
		if (it == device_line_staus.end())
		{
			return false;
		}

		TLineStatus& stat = device_line_staus.at(devID);
		if (!stat.m_bOPRet)
		{
			return false;
		}
		nret = (int)stat.m_LineStatus.camera_in_status[ccdID - 1];
	}
	catch (...)
	{
		return false;
	}

	return true;
}


EagleDataMgr::EagleDataMgr()
{
	record_list.clear();
}

EagleDataMgr::~EagleDataMgr()
{
	ClearRecordList();
	
}

EagleData_Record* EagleDataMgr::GetRecord(EagleData_Record_Id* id)
{
	EagleData_Record* return_record = 0;

	return_record = IsRecordExist(id);
	if (return_record == 0)
	{
		return_record = EagleData_GetRecordAndAllocMemory(*id);
		AddUnfreeRecord(return_record);
	}
	return return_record;
}

EagleData_Record* EagleDataMgr::GetRecord(std::wstring taskname, time_t start_time)
{
	EagleData_Record* rtnVal = 0;
	int nRecordNumber = EagleData_GetRecordNumber();
	EagleData_Record_Id* record_info = new EagleData_Record_Id[nRecordNumber];
	memset(record_info, 0, sizeof(EagleData_Record_Id)* nRecordNumber);

	nRecordNumber = EagleData_GetRecordList(record_info, nRecordNumber);
	int i = 0;
	for (i = 0; i < nRecordNumber; i++)
	{
		if (record_info[i].start_time == start_time)
		{
			if (taskname.compare(record_info[i].task_name) == 0)
			{
				break;
			}
		}
	}
	if (i != nRecordNumber)
		rtnVal = GetRecord(record_info + i);
	else
		rtnVal = 0;

	delete record_info;

	return rtnVal;
}

EagleData_CcdRecord* EagleDataMgr::GetCcdRecord(EagleData_Record_Id* id, int device_id, int serial)
{
	EagleData_Record* record = GetRecord(id);
	return GetCcdRecord(record, device_id, serial);
}

EagleData_CcdRecord* EagleDataMgr::GetCcdRecord(std::wstring taskname, time_t start_time, int device_id, int serial)
{
	return GetCcdRecord(GetRecord(taskname, start_time), device_id, serial);
}

EagleData_CcdRecord* EagleDataMgr::GetCcdRecord(EagleData_Record* task, int device_id, int serial)
{
	EagleData_CcdRecord* rtnVal = 0;

	if (task != 0)
	{
		rtnVal = task->ccd_record_list;
		while (rtnVal != 0)
		{
			if (device_id == rtnVal->id.device_id && serial == rtnVal->id.ccd_serial)
				break;
			rtnVal = rtnVal->next;
		}
	}
	return rtnVal;
}

void EagleDataMgr::ClearRecordList()
{
	for (auto it = record_list.begin(); it != record_list.end(); it++)
	{
		EagleData_FreeRecordMemory(*it);
	}
	record_list.clear();
}

EagleData_Record* EagleDataMgr::IsRecordExist(EagleData_Record_Id* id)
{
	EagleData_Record* rtnVal = 0;
	for (auto it = record_list.begin(); it != record_list.end(); it++)
	{
		if ((*it)->id.start_time == id->start_time 
			&& std::wstring((*it)->id.task_name).compare(id->task_name) == 0)
		{
			rtnVal = *it;
			break;
		}
	}
	return rtnVal;
}


void  RegisterExpListChangeCallback(exp_list_change_callback cb)
{
	CEagleDeviceMgr::GetInstance()->RegisterCcdListChangeCallback(cb);
}


ECaptureStatusType GetCurrentSystemStatus(bool GetReal)
{
	int current_status = 0;
	ECaptureStatusType status;
	if (GetReal)
	{
		std::vector<int> devices;
		CEagleDeviceMgr::GetInstance()->GetAllDevice(devices);
		for (auto dev_it = devices.begin(); dev_it != devices.end(); dev_it++)
		{
			status = CEagleDeviceMgr::GetInstance()->GetCurrentDeviceCaptureStatus(*dev_it);
			if (status == ECaptureRecording)
			{
				return ECaptureRecording;
			}
			current_status = status > current_status ? status : current_status;
		}
	}
	else
	{
		return CEagleDeviceMgr::GetInstance()->GetCurrentSystemCaptureStatus();
	}
	
	return (ECaptureStatusType)(current_status >= ECaptureMax ? ECaptureReady : current_status);
}

ECaptureStatusType GetCurrentSystemStatus(int device_id)
{
	return CEagleDeviceMgr::GetInstance()->GetCurrentDeviceCaptureStatus(device_id);
}

bool GetAllDevice(std::vector<int> &device)
{
	return CEagleDeviceMgr::GetInstance()->GetAllDevice(device);
}

bool GetCheckedDeviceList(std::vector<int> &device)
{
	return CEagleDeviceMgr::GetInstance()->GetCheckedDeviceList(device);
}

std::vector<eagle_capture_config> GetCcdList(int device_id)
{
	return CEagleDeviceMgr::GetInstance()->GetCcdList(device_id);
}

std::vector<eagle_capture_config> GetCheckedCcdList(int device_id)
{
	return CEagleDeviceMgr::GetInstance()->GetCheckedCcdList(device_id);
}

int RescanDevices(eagle_all_netcards *nics)
{
	return CEagleDeviceMgr::GetInstance()->RescanDevices(nics) == true ? 0 : -1;
}

void GetRealROIAndColorDepth(PIXEL_SAMPLING_TYPE SamplingType, int& column, int& line, int& color_depth)
{
	if (SamplingType > PixelSamplingGrey && SamplingType < PixelSamplingMax)
	{
		column /= 2;
	}
}